﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class TipoGestionRepository:MasterRepository,ITipoGestionRepository
    {
        public List<TIPO_GESTION> GetAllFromTipoGestion()
        {
            var query = from tg in context.TIPO_GESTION
                        select tg;
            return query.ToList();
        }

        public List<TIPO_GESTION> GetAllFromTipoGestionByDescripcion(string descripcion)
        {
            var query = from tg in context.TIPO_GESTION
                        select tg;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from tg in context.TIPO_GESTION
                        where tg.Descripcion.ToUpper().Contains(descripcion)
                        select tg;
            }
            return query.ToList();
        }

        public TIPO_GESTION GetFromTipoGestionById(int id)
        {
            var query = from tg in context.TIPO_GESTION
                        where tg.IdTipoGestion.Equals(id)
                        select tg;
            return query.SingleOrDefault();
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from tg in context.TIPO_GESTION
                        where tg.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 && tg.IdTipoGestion != id
                       select tg;
            return query.Any();
        }

        public void AddTipoGestion(TIPO_GESTION tipoGestion)
        {
            context.TIPO_GESTION.Add(tipoGestion);
            context.SaveChanges();
        }

        public void UpdateTipoGestion(TIPO_GESTION tipoGestion)
        {
            context.Entry(tipoGestion).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveTipoGestion(int id)
        {
            var existe = context.TIPO_GESTION.Find(id);

            if (existe != null)
            {
                context.TIPO_GESTION.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
